<%@ page pageEncoding="UTF-8"%>
<html>

<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>광장</title>
<script
	src="http://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js"></script>
<SCRIPT LANGUAGE="JavaScript">
<!--
	function go_URL(url)
	{
        location.href = url;
	}
//-->
</SCRIPT>
</head>

<%

if(session.getAttribute("roomNo") == null) {
	session.setAttribute("roomNo", "1");
}
if(session.getAttribute("guildNo") == null ) {
	session.setAttribute("guildNo", "-1");
}	
if(session.getAttribute("charNo") == null) {
	session.setAttribute("charNo", "1");
}
if(session.getAttribute("serverNo") == null) {
	session.setAttribute("serverNo", "1");
}

%>
<body>
	<table width="50%" cellpadding="0" cellspacing="0">
		<tr>
			<td width="700" colspan="4">
				<div style="height: 400px; overflow: auto;">
					<table id="chat">
					</table>
				</div></td>
		</tr>
		<tr>
			<script>
				function addmessage() {
					new Ajax.Request('../post/postChattingAddMsg.jsp', {
						method : 'post',
						parameters : $('form').serialize(),
						onSuccess : function(transport) {
							$('msgType').value = '';
						}
					});
				}
			</script>

			<script>
				function updateTired() {
					new Ajax.Request(
							'../post/postChangeCharacterStatus.jsp',
							{
								onSuccess : function(transport) {
									var messages = transport.responseXML
											.getElementsByTagName('message');
									for ( var i = 0; i < messages.length; i++) {
										var tired = parseInt(messages[i].firstChild.nodeValue);
										var delta = parseInt(messages[i]
												.getAttribute('delta'));
										var elTR = $('chat').insertRow(-1);
										var elTD1 = elTR.insertCell(-1);
										elTD1.appendChild(document
												.createTextNode("[변경알림]"));
										var elTD2 = elTR.insertCell(-1);
										elTD2.appendChild(document
												.createTextNode("피로도가 " + tired
														+ "이 되었습니다."));
									}
								}
							});
				}
			</script>


			<form name="form">
				<td width="78"><select name="chatCD" size="1">
						<option value="CT001">방채팅</option>
						<option value="CT002">귓속말</option>
						<option value="CT003">길드채팅</option>
				</select>
				</td>
				<td width="147">
					<p>
						<span style="font-size: 9pt;"><input type="text"
							name="toWho" maxlength="20" size="20">
						</span>
					</p></td>
				<td width="482">
					<p>
						<span style="font-size: 9pt;"><input type="text"
							name="chatType" id="msgType" size="75">
						</span>
					</p></td>
				<td width="10">
					<p>
						<span style="font-size: 9pt;"><button
								onclick="addmessage()">Add</button>
						</span>
					</p></td>
		</tr>
	</table>
	</form>

	<script>
		var lastid = 0;
		var tiredCount = 0;
		function getMessages() {
			new Ajax.Request(
					'../post/postChattingMessage.jsp?lastid=' + lastid,
					{
						onSuccess : function(transport) {
							var messages = transport.responseXML
									.getElementsByTagName('message');
							for ( var i = 0; i < messages.length; i++) {
								var message = messages[i].firstChild.nodeValue;
								var id = parseInt(messages[i]
										.getAttribute('id'));
								var serverNo = messages[i]
										.getAttribute('serverNo');
								var senderCharNo = messages[i]
										.getAttribute('senderCharNo');
								var senderName = messages[i]
										.getAttribute('senderName');
								var chatCdClassify = messages[i]
										.getAttribute("chatCdClassify");
								var chatCD = messages[i].getAttribute('chatCD');
								if (chatCD == "CT001") {
									chatCD = "방채팅";
								} else if (chatCD == "CT002") {
									chatCD = "귓속말";
								} else {
									chatCD = "길드채팅";
								}

								if (id > lastid) {
									var elTR = $('chat').insertRow(-1);
									var elTD1 = elTR.insertCell(-1);
									elTD1.appendChild(document
											.createTextNode("[" + senderName
													+ "]"));
									var elTD2 = elTR.insertCell(-1);
									elTD2.appendChild(document
											.createTextNode("[" + chatCD
													+ "] : "));
									var elTD3 = elTR.insertCell(-1);
									elTD2.appendChild(document
											.createTextNode(message));

									lastid = id;
								}
							}
							tiredCount++;
							if (tiredCount >= 5) {
								updateTired();
								tiredCount = 0;
							}
							window.setTimeout(getMessages, 1000);
						}
					});
		}
		getMessages();
	</script>

	<p><span style="font-size:9pt;"><input type="button" value="광장 나가기" onclick='go_URL("leaveRoom.jsp"); return false;'></span></p>
</body>

</html>